<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * keyword search controller
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (So, 27 Feb 2011) $ / $Author: armand.turpel $
 */

namespace Geocontexter\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractController;

class KeywordSearchController extends AbstractController
{
    public function init()
    {
        $this->initView( 'geocontexter/keyword-search/index.phtml' );

        $this->view->setTerminal(true);

        $this->renderer->headTitle('Search for keywords' , 'PREPEND');

        $this->view->list_result = array();
        $this->view->error       = array();
    }

    public function indexAction()
    {

        $this->view->search = $search = $this->request->getPost()->value;
        $this->view->type   = $type   = $this->request->getPost()->type;
        $page                         = $this->request->getPost()->page;

        if (null === $search) {
            $this->view->search = $search = $this->params()->fromRoute('value',false);
        }

        $itemCountPerPage = 3;

        if ((null === $page)) {
            $page = $this->params()->fromRoute('page',false);
        }

        if ((false === $page) || (1 == $page)) {
            $page = 0;
        }

        $KeywordSearch = $this->CoreModel('KeywordSearch');

        $totalNumRows = $KeywordSearch->totalNumRows(array('search' => $search));

        $params  = array('search'             => $search,
                         'default_display'    => true,
                         'system_serial'      => true,
                         'limit'              => array($itemCountPerPage,$page));

        $this->view->result = $KeywordSearch->run( $params );

        // build paginator result
        //
        $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\Null($totalNumRows));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setPageRange(10);
        \Zend\Paginator\Paginator::setDefaultScrollingStyle('Sliding');
        \Zend\View\Helper\PaginationControl::setDefaultViewPartial(
            'paginator-slide-keyword'
        );

        //this is paginator
        $this->view->paginator = $paginator;

        return $this->view;
    }
}

